package com.rljk.service.impl;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

import com.alibaba.fastjson.JSONObject;
import com.rljk.redis.RedisService;
import com.rljk.service.AssetsManager;
import com.rljk.utils.Const;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import com.rljk.common.dao.DaoSupport;
import com.github.pagehelper.PageHelper;
import com.rljk.domain.common.PageData;

/** 
 * 说明： 个人资产表
 * 创建人：zhaozhenhua
 * 创建时间：2018-07-21
 * @version
 */
@Service("assetsService")
@CacheConfig(cacheNames = "assetscache")
public class AssetsService implements AssetsManager {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Autowired
	RedisService redisService;
	
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		dao.save("AssetsMapper.save", pd);
	}
	
	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		dao.delete("AssetsMapper.delete", pd);
	}
	
	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		dao.update("AssetsMapper.edit", pd);
	}
	
	/**分页列表
	 * @param currentPage
	 * @param pageSize
	 * @param pd
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listPage(int currentPage,int pageSize,PageData pd)throws Exception{
	    PageHelper.startPage(currentPage,pageSize);
		return (List<PageData>)dao.findForList("AssetsMapper.listAll", pd);
	}
	
	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("AssetsMapper.listAll", pd);
	}
	
	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("AssetsMapper.findById", pd);
	}
	
	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("AssetsMapper.deleteAll", ArrayDATA_IDS);
	}

	/**app获取我的宝石排位
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> getGemRank(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("AssetsMapper.getGemRank", pd);
	}

    /**
     * 向redis中缓存a获取我的宝石排位
     */
	public void setGemRankCache()throws Exception{
        List<PageData> gemRank = getGemRank(null);
        for (PageData p : gemRank) {
            redisService.zadd(Const.MYSTONE,p.getDouble("XW_VALUE"), net.sf.json.JSONObject.fromObject(p).toString());
        }
        redisService.expire(Const.MYSTONE,Const.MYSTONE_EXPIRE);
    }

	/**
	 *  根据XW链进行排名
	 */
	public Set<String> getGemRankCache()throws Exception{
        return redisService.zrevrange(Const.MYSTONE, 0, -1);
    }

	
}

